package cn.chinasyq.photoquan.photo.adapter;

import android.app.Activity;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.toolbox.NetworkImageView;

import java.util.ArrayList;
import java.util.List;

import cn.chinasyq.photoquan.R;
import cn.chinasyq.photoquan.photo.bean.GameInfo;
import cn.master.util.utils.ScreenUtil;
import cn.master.volley.commons.VolleyHelper;

/**
 * Created by 一搏 on 2015/11/24.
 */
public class GamePhotoListAdapter extends BaseAdapter {

    public List<GameInfo> getPhotoEntities() {
        return photoEntities;
    }

    public void setPhotoEntities(List<GameInfo> photoEntities) {
        this.photoEntities = photoEntities;
        notifyDataSetChanged();
    }

    public void addPhotoEntities(List<GameInfo> photoEntities) {
        this.photoEntities.addAll(photoEntities);
        notifyDataSetChanged();
    }

    private List<GameInfo> photoEntities;
    private int itemWidth, itemHeight;

    public GamePhotoListAdapter(Activity activity) {
        photoEntities = new ArrayList<>();
        DisplayMetrics m = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(m);
        itemWidth = (int) ((ScreenUtil.getScreenWidth(activity) - 3 * TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, m)) / 2);
        itemHeight = (int) (itemWidth * (9f / 16f));
    }

    @Override
    public int getCount() {
        return photoEntities.size();
    }

    @Override
    public Object getItem(int position) {
        return photoEntities.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        GameInfo entity = (GameInfo) getItem(position);
        return getOneImageView(entity, convertView, parent, position);
    }


    private View getOneImageView(GameInfo entity, View convertView, ViewGroup parent, int position) {
        ViewHolderOneImage viewHolder = new ViewHolderOneImage();
        if (position % 2 == 0) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_game_photography_exhibition, null);
        } else {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_game_photography_exhibition2, null);
        }

        viewHolder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
        viewHolder.tv_author = (TextView) convertView.findViewById(R.id.tv_author);
        viewHolder.tv_vote_count = (TextView) convertView.findViewById(R.id.tv_vote_count);
        viewHolder.iv_image = (NetworkImageView) convertView.findViewById(R.id.iv_image);
        viewHolder.iv_image.setScaleType(ImageView.ScaleType.FIT_XY);
        viewHolder.iv_image.setLayoutParams(new LinearLayout.LayoutParams(itemWidth, itemHeight));
        convertView.setTag(viewHolder);
        viewHolder.tv_title.setText(entity.getTitle());
        viewHolder.iv_image.setImageUrl(entity.getImage(), VolleyHelper.getImageLoader());
        viewHolder.tv_author.setText("创作者："+entity.getAuthor());
        viewHolder.tv_vote_count.setText("得票数："+entity.getVotes_count());
        return convertView;
    }


    class ViewHolderOneImage {
        TextView tv_title,tv_author,tv_vote_count;
        NetworkImageView iv_image;
    }

}
